<?php


class TemplateHandler {

	var $tfile = "";

	var $error = "";

	var $tagmas = array();

	var $Necho = 1;

	function TemplateHandler($filename=MainTemplate) {

        global $SLANG;
        global $VIEW;
        global $SERVER;
        global $lang;
        global $LaunchURL;
        global $PureLaunchURL;
        global $p;
        global $pilot;
        global $navigation;
        global $VARS;

		if(!file_exists($filename)) {

            $this->error = "Template file '$filename' does not exist.
                        <br>Please check paths in genconfig.php and folder permissions.";

        }

        else {

        	ob_start();

            include "$filename";

	       	$c = ob_get_contents();

            ob_end_clean();

	        $this->tfile = $c;

            $this->Necho = 1;
        }
    }


    function AssignTag($tagname, $value) {

    	$this->tagmas["$tagname"] = $value;

    }

    function AssignTagPlus($tagname, $value) {

    	$this->tagmas["$tagname"] .= $value;

    }

    function Parse() {

    	global $SiteURL;
		global $PureSiteURL;
    	global $ImageURL;
    	global $StyleURL;
        global $LaunchURL;
        global $PureLaunchURL;
        global $_SERVER;
        global $navigation;


    	if ($this->error != "" || $this->Necho == 0) {

        	return $this->error;

        }


    	$c = $this->tfile;


    	foreach ($this->tagmas as $tag=>$value) {

			$c = str_replace($tag, $value, $c);

        }

        $c = str_replace("<!--SiteURL-->",$SiteURL,$c);
        $c = str_replace("<!--PureSiteURL-->",SiteURL,$c);
        $c = str_replace("<!--ImageURL-->",$ImageURL,$c);
        $c = str_replace("<!--StyleURL-->",$StyleURL,$c);
        $c = str_replace("<!--LaunchURL-->",$LaunchURL,$c);
        $c = str_replace("<!--PureLaunchURL-->",$PureLaunchURL,$c);

        return $c;
    }

}


?>